package org.omich.loaves.gui.view;

import org.omich.loaves.R;
import org.omich.loaves.gui.GuiLoaf;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class LoavesListAdapter extends BaseAdapter
{
	private GuiLoaf [] mLoaves = new GuiLoaf[0];
	private Context mContext;
	
	public LoavesListAdapter (Context context)
	{
		mContext = context;
	}
	
	public void setLoaves (GuiLoaf [] loaves)
	{
		mLoaves = loaves;
		super.notifyDataSetChanged();
	}

	//==== BaseAdapter implemementation =======================================
	@Override
	public int getCount() {return mLoaves.length;}

	@Override
	public Object getItem(int position)	{return mLoaves[position];}

	@Override
	public long getItemId(int position) {return mLoaves[position].name;}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View v = convertView;
		
		if(v == null)
		{
			LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.loaveslist_item, null);
		}

		TextView textView = (TextView) v.findViewById(R.id.loaveslist_item_weight);
		textView.setText(Integer.toString(mLoaves[position].weight / 1000));

		return v;
	}
}
